<template>
  <div class="su-goodsManage">
    <!--表单-->
    <el-form>
      <el-form-item label="商品名称">
        <el-input v-model="formData.aName" style="width: 300px" placeholder="请输入商品名称"></el-input>
      </el-form-item>
      <attr-search
        ref='attrSearch' aType='0'
      />
      <el-button @click="clickSearch">搜索</el-button>
      <el-button type='primary' @click="$router.push({name: 'sys_goods_detail'})">添加商品</el-button>
    </el-form>

    <!--表格-->
    <a-table :cols="cols" :data="data" style="margin-top: 20px">
      <el-table-column align="center" label="操作" width='150' slot='ahead'>
        <template slot-scope="scope">
          <div>
            <el-button size='mini' @click='choose(scope.row.id)'>修改</el-button>
            <el-button size='mini' @click='deleteObj(scope.row.id)'>删除</el-button>
          </div>
        </template>
      </el-table-column>
    </a-table>

    <div class='u-pagination'>
      <!-- 分页 -->
      <pagination ref='page' @change='getData' :action='fetchData' :pageSize='20'></pagination>
    </div>
  </div>
</template>
<script>
  import {mapMutations} from 'vuex'
  import AttrSearch from '@/components/business/AttrSearch'
  export default {
    name: 'sm-goodsManage',
    components: {
      AttrSearch
    },
    data() {
      return {
        formData: {
          aName: ''
        },
        data: [],
        cols: [
          {label: '商品名称', prop: 'name'},
          {label: '属性类别', prop: 'title'},
          {label: '价格', prop: 'price'}
        ]
      }
    },
    mounted() {
      this.$refs.page.changePage()
    },
    methods: {
      ...mapMutations(['setTabRoutes']),

      routerPush(type, args) {
        // push 到缓存里
        this.setTabRoutes({type: 'add', obj: {...this.getRouterInfoAccordingName(type), ...args}})
      },
      async fetchData(aPageNum, aPageSize) {
        const params = {
          aMerchantId: this.aFirmId,
          aPageNum,
          aPageSize
        }
        if (this.formData.aName) {
          params.aName = this.formData.aName
        }
        params.aAttrs = this.$refs.attrSearch.getData()
        return this.$api.supplyGetMines(params)
      },

      getData(data) {
        this.data = data
      },
      deleteObj(id) {
        if (this.ifRequesting) return false
        this.__messageBox({
          title: '你确认要删除该商品吗？',
          successCallback: async () => {
            this.ifRequesting = true
            const {code} = await this.$api.supplyDelete({aId: id})
            this.ifRequesting = false
            this.$refs.page.changePage()
            return code
          }
        })
      },
      clickSearch() {
        this.$refs.page.changePage()
      },
      choose(id) {
        const query = {id}
        this.routerPush('sys_goods_detail', {query})
        this.$router.push({name: 'sys_goods_detail', query})
      }
    }
  }
</script>
